/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ParTreeVisualizer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.JFrame;

/**
 *
 * @author mwh
 */
public class SentenceFrame extends JFrame {

    BilingualPanel panel;

    PrintRequestAttributeSet attributes;

    ButtonPanel bPanel;

    int listIndex;

    List<SentenceAlignement> sentenceAlignements;


    public void rePaintAlignment() {
        panel.repaint();
    }
    
    public SentenceFrame(List<SentenceAlignement> sal) throws FileNotFoundException, PrintException {
        setTitle("Alignement output");
        setSize(1024, 250);

        sentenceAlignements = sal;


        listIndex = 0;
        SentenceAlignement sa = sal.get(listIndex);
        panel = new BilingualPanel(sa);

        

        attributes = new HashPrintRequestAttributeSet();


        add(panel, BorderLayout.CENTER);

        bPanel = new ButtonPanel(new nextAction(), new prevAction(), new printAction());

        add(bPanel, BorderLayout.SOUTH);

//        pPanel = new PrintPanel(new nextAction());

        //  add(pPanel, BorderLayout.SOUTH);
        updateButton();

        attributes.add(OrientationRequested.LANDSCAPE);
        attributes.add(MediaSizeName.ISO_A4);




    }


    public void updateButton() {

        if (listIndex < sentenceAlignements.size() - 1) {
            bPanel.setNext(true);
        } else {
            bPanel.setNext(false);
        }


        if (listIndex > 0) {
            bPanel.setPrevious(true);
        } else {
            bPanel.setPrevious(false);
        }


    }

    class printAction implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {


            FilenameFrame frame = new FilenameFrame("output.ps");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            try {
                printPS("output.ps");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(SentenceFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (PrintException ex) {
                Logger.getLogger(SentenceFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    class nextAction implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {

            SentenceFrame.this.remove(panel);

            listIndex++;
            SentenceAlignement sa = sentenceAlignements.get(listIndex);

            panel = new BilingualPanel(sa);
            add(panel);
            validate();
            repaint();

            updateButton();
        }
    }

    class prevAction implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {

            SentenceFrame.this.remove(panel);

            listIndex--;
            SentenceAlignement sa = sentenceAlignements.get(listIndex);

            BilingualPanel newpanel = new BilingualPanel(sa);
            add(newpanel);
            validate();
            repaint();
            panel = newpanel;

            updateButton();

        }
    }


    public void printPS(String fn) throws FileNotFoundException, PrintException {
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        String mimeType = "application/postscript";
        StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType);

        FileOutputStream out = new FileOutputStream(fn);
        if (factories.length == 0) {
            return;
        }
        StreamPrintService service = factories[0].getPrintService(out);

        Doc doc = new SimpleDoc(panel, flavor, null);
        DocPrintJob job = service.createPrintJob();
        job.print(doc, attributes);
    }
}
